/**
 * Created by LG on 2017/9/20.
 */

function export2(url)
{
    window.location.href = url;
    layer.closeAll();
}

//取消
function layuihide() {
	$(".layui-layer").remove();
	$(".layui-layer-shade").remove();
}
//总后台
/*
 *   合同详情
 */
//备注添加成功
function Addremarksuccess(url, i, contract_id) {
	var remark = $("#remark_" + i + "").val();
	var equipments_id = $("#remark_id_" + i + "").val();
	// console.log(remark)
	$.get(url, {
		remark: remark,
		id: i,
		contract_id: contract_id,
		equipments_id: equipments_id
	}, function(flag) {
		// console.log(flag)
		if(flag.code == 1) {
			layer.msg(flag.msg, {
				time: 1000,
				icon: 1
			});
			$("#mainFrame").attr('src', '/admin/contractinfo/details?contract_id=' + contract_id + '&rand=' + Math.random() + '#equipment');
		} else {
			layer.msg(flag.msg, {
				time: 1000,
				icon: 0
			});
		}
	});
	$(".layui-layer-shade").remove();
	$(".layui-layer").remove();

}

//修改设备成功
function modifyinformationsuccess(url, i, contract_id) {
	// console.log(i)
	var equipments_name = $("#equipments_name_" + i + "").val();
	var equipments_number = $("#equipments_number_" + i + "").val();
	var equipments_id = i;
	var reg = /^[0-9]*[1-9][0-9]*$/;
	if(equipments_name == '') {
		parent.layer.msg("请选择设备型号", {
			time: 1000,
			icon: 0
		})
		return false;
	}
	if(!reg.test(equipments_number)) {
		$("#equipments_number_" + i + "").val('');
		parent.layer.msg("请输入正确的设备编号", {
			time: 1000,
			icon: 0
		})
		return false;
	}
	$.get(url, {
		id: i,
		contract_id: contract_id,
		equipments_number: equipments_number,
		equipments_name: equipments_name,
		equipments_id: equipments_id
	}, function(flag) {
		console.log(flag)
		if(flag.code == 1) {
			layer.msg(flag.msg, {
				time: 1000,
				icon: 1
			});
			$("#mainFrame").attr('src', '/admin/contractinfo/details?contract_id=' + contract_id + '&rand=' + Math.random() + '#equipment');
		} else {
			layer.msg(flag.msg, {
				time: 1000,
				icon: 0
			});
		}
	});
	$(".layui-layer-shade").remove();
	$(".layui-layer").remove();

}
//取消
function layuihides() {
	$(".layui-layer-shade").remove();
	$(".layui-layer").remove();
}

//---------------------------------------END----------------------------------------------------

//行政区域

//备注添加成功
function Addremarksuccess_Administrative(url, i, contract_id) {

	var remark = $("#remark_" + i + "").val();
	var equipments_id = $("#remark_id_" + i + "").val();
	// console.log(remark)
	$.get(url, {
		id: i,
		contract_id: contract_id,
		remark: remark,
		equipments_id: equipments_id
	}, function(flag) {
		// console.log(flag)
		if(flag.code == 1) {
			layer.msg(flag.msg, {
				time: 1000,
				icon: 1
			});
			$("#mainFrame").attr('src', '/administrative/contractinfo/details?contract_id=' + contract_id + '&rand=' + Math.random() + '#equipment');
		} else {
			layer.msg(flag.msg, {
				time: 1000,
				icon: 0
			});
		}
	});
	$(".layui-layer-shade").remove();
	$(".layui-layer").remove();

}

//修改设备成功
function modifyinformationsuccess_Administrative(url, i, contract_id) {
	// console.log(i)
	var equipments_name = $("#equipments_name_" + i + " option:selected").val();
	var equipments_id = i;
	var equipments_number = $("#equipments_number_" + i + "").val();
	var reg = /^[0-9]*[1-9][0-9]*$/;
	if(equipments_name == '') {
		parent.layer.msg("请选择设备型号", {
			time: 1000,
			icon: 0
		})
		return false;
	}
	if(!reg.test(equipments_number)) {
		$("#equipments_number_" + i + "").val('');
		parent.layer.msg("请输入正确的设备编号", {
			time: 1000,
			icon: 0
		})
		return false;
	}
	$.get(url, {
		id: i,
		contract_id: contract_id,
		equipments_number: equipments_number,
		equipments_name: equipments_name,
		equipments_id: equipments_id
	}, function(flag) {
		console.log(flag)
		if(flag.code == 1) {
			layer.msg(flag.msg, {
				time: 1000,
				icon: 1
			}, function() {
				$(".layui-layer-shade").remove();
				$(".layui-layer").remove();
			});
			$("#mainFrame").attr('src', '/administrative/contractinfo/details?contract_id=' + contract_id + '&rand=' + Math.random() + '#equipment');
		} else {
			layer.msg(flag.msg, {
				time: 1000,
				icon: 0
			}, function() {
				$(".layui-layer-shade").remove();
				$(".layui-layer").remove();
			});
		}
	});
	$(".layui-layer-shade").remove();
	$(".layui-layer").remove();
}

//取消
function cancelmodel() {
	$('.layui-layer-shade').remove();
	$('.layui-layer').remove();
}

//删除合同确定
function delconfirm(obj) {
	layer.msg('删除成功', {
		time: 1000,
		icon: 1
	})
	$(obj).parent().remove();
	$('.layui-layer-shade').remove();
	$('.layui-layer').remove();
}

//弹窗选择
function subback(type) {
	var user_id = $("input[name='user_id']:checked").val();
	if(type == 2) {
		var telphone = $("#telphone" + user_id).val();
		console.info(telphone);
		$('#mainFrame').contents().find("#customer_introducer_name").val(telphone);
		$('#mainFrame').contents().find("#introducer_id").val(user_id);
	} else {
		var username = $("#cusername" + user_id).val();
		$('#mainFrame').contents().find("#customer_name").val(username);
		$('#mainFrame').contents().find("#user_id").val(user_id);
	}
	cancelmodel();
}
//弹窗搜索
function searchCustomer() {
	document.getElementById("mainFrame").contentWindow.Ajaxpage();
}

function jumpcustome() {
	$("#mainFrame").attr('src', '/administrative/customerinfo/add' + '#equipment');
	cancelmodel();
}

// 指派工程人员弹窗，新增工程人员按钮
function engineeradd() {
    $("#mainFrame").attr('src', '/administrative/engineer/add' + '#equipment');
    cancelmodel();
}

// 获取选中的用户
function qwer() {
	$("#list input").each(function() {
		if($(this).is(":checked")) {
			var username = $(this).val();
			var userid = $(this).attr('data');
			var telphone = $(this).attr('telphone');
			var id_card = $(this).attr('id_card');
			// 将选中用户id存入隐藏域
			$('#mainFrame').contents().find("#user_id").val(userid);
			// 将选中用户姓名显示在input中
			$('#mainFrame').contents().find("input[name='_username']").val(username);
			// 用户电话
			$('#mainFrame').contents().find("input[name='telphone']").val(telphone);
			// 用户身份证
			$('#mainFrame').contents().find("input[name='id_card']").val(id_card);
			// 清空合同编号
			$('#mainFrame').contents().find("input[name='contract_no']").val('');
			// 清空装机时间
			$('#mainFrame').contents().find("input[name='installed_time']").val('');
			// 清空续费时间
			$('#mainFrame').contents().find("input[name='renew_date']").val('');
			// 清空到期时间
			$('#mainFrame').contents().find("input[name='exire_date']").val('');
			// 清空合同id隐藏域
			$('#mainFrame').contents().find("#contract_id").val('');
            //合同地址
            // $('#mainFrame').contents().find("#_province").val('');
            // $('#mainFrame').contents().find("#_city").val('');
            $('#mainFrame').contents().find("#_area").val('');
            $('#mainFrame').contents().find("#addressDetail").val('');
		}
	})
	$('.layui-layer-shade').remove();
	$('.layui-layer').remove();
}
// 获取选中的合同
function contractqr() {

	$("#clist input").each(function() {

		if($(this).is(":checked")) {
			var contract_no = $(this).val();
			var contract_id = $(this).attr('contract_id');
			var installed_time = $(this).attr('installed_time');
			var renew_date = $(this).attr('renew_date');
            var province = $(this).attr('province');
            var exire_date = $(this).attr('exire_date');
            var city = $(this).attr('city');
            var area = $(this).attr('area');
            var address = $(this).attr('address');

            $('#mainFrame').contents().find("#sCity").val(city);
            $('#mainFrame').contents().find("#sArea").val(area);
			// 将选中合同id存入隐藏域
            $('#mainFrame').contents().find("#contract_id").val(contract_id);
			// 将选中的合同编号显示在input中
			$('#mainFrame').contents().find("input[name='contract_no']").val(contract_no);
			// 装机时间
			$('#mainFrame').contents().find("input[name='installed_time']").val(installed_time);
			// 续费周期
			$('#mainFrame').contents().find("input[name='renew_date']").val(renew_date);
			// 到期时间
			$('#mainFrame').contents().find("input[name='exire_date']").val(exire_date);
            //合同地址
            $('#mainFrame').contents().find("#sProvince").val(province).change();
            $('#mainFrame').contents().find("#addressDetail").val(address);
            //
			// console.log('>>>>>>>>>>','begin')
			// console.log($(this).attr('is_explain'));
			//存在讲解的时候，选择工程人员
			if ($(this).attr('is_explain') == 1){
                $('#mainFrame').contents().find(".engineers_box").show();
                $('#mainFrame').contents().find("#is_explain").val(1);
			}
            document.getElementById("mainFrame").contentWindow.equipmentssss(contract_id);
		}
	})
	$('.layui-layer-shade').remove();
	$('.layui-layer').remove();
}

// 用户模糊搜索事件
function search() {
	// 调用分页
	document.getElementById("mainFrame").contentWindow.Ajaxpage(1);
}
// 合同模糊搜索事件
function search_() {
	// 调用分页
	document.getElementById("mainFrame").contentWindow.Ajaxpages(1);
}
// 工单指派工程人员确认按钮操作
function engineersqr(work_id) {
	$("#elist input").each(function() {
		if($(this).is(":checked")) {
			var engineers_id = $(this).val(); // 工程人员id
			var engineers_name = $(this).attr('data'); // 工程人员姓名
			var engineers_number = $(this).attr('number'); // 工程人员编号
			var engineers_phone = $(this).attr('telphone'); // 工程人员电话
			var detail = $(this).attr('detail'); // 详情页面
			// 表示详情页面直接指派
			if(detail == 1) {
				// 调用处理指派任务方法
				document.getElementById("mainFrame").contentWindow.Handles(engineers_id, engineers_number, engineers_name, engineers_phone);
				// 调用AssignResult方法，处理指派结果
				document.getElementById("mainFrame").contentWindow.AssignResults();
				var srcs = "/administrative/workorder/detail?id=" + work_id;
				$("#mainFrame").attr("src", srcs)
			} else {
				// 调用处理指派任务方法
				document.getElementById("mainFrame").contentWindow.Handle(engineers_id, engineers_number, engineers_name, engineers_phone);
				// 调用AssignResult方法，处理指派结果
				document.getElementById("mainFrame").contentWindow.AssignResult();
				// 调用分页
				document.getElementById("mainFrame").contentWindow.Ajaxpage(1);
			}
		}
	});
	$('.layui-layer-shade').remove();
	$('.layui-layer').remove();
}

//解绑设备
//id	解绑设备ID
//url	后端地址
//type	端
function unBind(url, id, contract_id, type) {
	// console.log(contract_id)
	// console.log(type)
	$.get(url, {
		id: id
	}, function(flag) {
		if(flag.code == 1) {
			layer.msg(flag.msg, {
				time: 1000,
				icon: 1
			});
			$("#mainFrame").attr('src', '/' + type + '/contractinfo/details?contract_id=' + contract_id + '&rand=' + Math.random() + '#equipment');
		} else {
			layer.msg(flag.msg, {
				time: 1000,
				icon: 0
			});
		}
		$(".layui-layer-shade").remove();
		$(".layui-layer").remove();
	});
}
//通过工程人员id 查询对应工单数目
function jed(id, name, type, status) {
	var uid = '#a' + id;
	if(type == 1) {
		$(uid, window.parent.document).parent().click();
		$(uid, window.parent.document).click();
		$("#gcry", window.parent.document).val(name);
		$("#gstatus", window.parent.document).val(status);
		$('#mainFrame', window.parent.document).attr("src", '/admin/workorder/assigned');
	}
	if(type == 2) {
		$(uid, window.parent.document).parent().click();
		$(uid, window.parent.document).click();
		$("#gcry", window.parent.document).val(name);
		$("#gstatus", window.parent.document).val(status);
		$('#mainFrame', window.parent.document).attr("src", '/administrative/workorder/assigned');
	}
	if(type == 3) {
		$(uid, window.parent.document).parent().click();
		$(uid, window.parent.document).click();
		$("#gcry", window.parent.document).val(name);
		$("#gstatus", window.parent.document).val(status);
		$('#mainFrame', window.parent.document).attr("src", '/operation/workorder/assigned');
	}
}

//优惠使用
function recommend(id, cid) {
	var log = $('#log').val();
	var tem = 1;
	if(log == null || log == undefined || log == '') {
		top.layer.msg('请填写备注！', {
			icon: 2
		});
		tem = 0;
	}
	if(id == null || id == '' || id == undefined) {
		top.layer.msg('请选择推荐设备后操作！', {
			icon: 2
		});
		tem = 0;
	}
	if(tem == 1) {
		$.getJSON('/administrative/customerinfo/recommend', {
			id: id,
			log: log
		}, function(data) {
			top.layer.msg(data.message, {
				icon: data.status,
				time: 300
			}, function() {
				$('#mainFrame', window.parent.document).attr("src", '/administrative/customerinfo/devicedetails/cid/' + cid);
				$('.layui-layer-shade').remove();
				$('.layui-layer').remove();
			});
		});
	}
}

//Firefox兼容
function Browser() {
	var explorer = navigator.userAgent;
	if(explorer.indexOf("Firefox") >= 0) {
		$('#overflow', window.parent.document).css('overflow-y', 'hidden');
	}
}
Browser()

//限制翻页
function inputlength() {
	var pagenum = $("#allpage i").html();
	$('.laypage_total input').attr('max', pagenum);
	var text = document.getElementsByClassName('laypage_skip')[0];
	if(text) {
		text.onkeyup = function() {
			this.value = this.value.replace(/\D/g, '');
			if(text.value > pagenum) {
				text.value = pagenum;
			}
		}
	}
}